//
//  UUOrderDetailConfirmVC.m
//  UUCharterSupplier
//
//  Created by ZhangAo on 25/02/2018.
//  Copyright © 2018 UUCharter. All rights reserved.
//

#import "UUOrderDetailConfirmVC.h"
#import "UUResourceManager.h"
#import "UUOrderManager.h"
#import "UUPermissionManager.h"

@interface UUOrderDetailConfirmVC ()

@property (nonatomic, strong) UIButton *rejectButton;
@property (nonatomic, strong) UIButton *approveButton;

@end

@implementation UUOrderDetailConfirmVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    DKAssert(self.orderId != nil);
   
    self.rejectButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.rejectButton setTitle:@"无法接单" forState:UIControlStateNormal];
    self.rejectButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.rejectButton setTitleColor:COMMON_TEXT_ORANGE_COLOR forState:UIControlStateNormal];
    [self.rejectButton setBackgroundImage:GetResourceManager().buttonImage3 forState:UIControlStateNormal];
    [self.rejectButton setBackgroundImage:GetResourceManager().buttonImage3HL forState:UIControlStateHighlighted];
    [self.rejectButton addTarget:self action:@selector(rejectOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.rejectButton];
    [self.rejectButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(15);
        make.height.mas_equalTo(45);
        make.centerY.mas_equalTo(0).offset(-20);
    }];
    
    self.approveButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.approveButton setTitle:@"确定接单" forState:UIControlStateNormal];
    self.approveButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [self.approveButton setBackgroundImage:GetResourceManager().buttonImage1 forState:UIControlStateNormal];
    [self.approveButton setBackgroundImage:GetResourceManager().buttonImage1HL forState:UIControlStateHighlighted];
    [self.approveButton addTarget:self action:@selector(approveOrder) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:self.approveButton];
    [self.approveButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.rejectButton.mas_right).offset(20);
        make.right.mas_equalTo(-15);
        make.centerY.height.equalTo(self.rejectButton);
        make.width.equalTo(self.rejectButton).multipliedBy(1.3);
    }];
    
    if (![GetPermissionManager() checkPermission:UUPermissionFlagOrderReadWrite]) {
        self.rejectButton.enabled = NO;
        self.approveButton.enabled = NO;
    }
    
    self.preferredContentSize = CGSizeMake(0, 20 + 45 + 40);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)rejectOrder {
    [UIAlertController presentAlertControllerWithTitle:@"生意不等人，请三思而行。"
                                               message:nil
                                             doneTitle:@"我再想想"
                                           cancelTitle:@"决意不接"
                                         completeBlock:^(BOOL isCancelled) {
                                             if (isCancelled) {
                                                 [self updateOrderWithConfirm:NO];
                                             }
                                         }];
}

- (void)approveOrder {
    [UIAlertController presentAlertControllerWithTitle:@"确认接单后不可取消订单，确定？" message:nil doneBlock:^{
        [self updateOrderWithConfirm:YES];
    } cancelBlock:nil];
}

- (void)updateOrderWithConfirm:(BOOL)confirm {
    ShowIndicatorInKeyWindow();
    [GetOrderManager() asyncConfirmOrderWithOrderId:self.orderId
                                            confirm:confirm
                                      completeBlock:^(NSError *error) {
                                          HideIndicatorInKeyWindow();
                                          
                                          if (error == nil) {
                                              
                                          } else {
                                              ShowTipsInKeyWindow(error.localizedDescription);
                                          }
                                      }];
}

@end
